package com.itany.shop.controller;

import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.vo.Cart;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author MissChen
 * @date 2024年05月16日 15:40
 */
@WebServlet("*.cart")
public class CartServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String path = request.getServletPath();
        switch (path) {
            case "/add.cart":
                addCart(request, response);
                break;
            case "/modify.cart":
                modifyCart(request, response);
                break;
            case "/removeById.cart":
                removeById(request, response);
                break;
            case "/removeByIds.cart":
                removeByIds(request, response);
                break;
            case "/clear.cart":
                clear(request, response);
                break;
        }

    }

    public void addCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        //从session中取出购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");

        //获取请求参数
        String id =  request.getParameter("id");
        try {
            System.out.println(id);
            cartService.addCart(cart, Integer.parseInt(id));
            response.sendRedirect(request.getContextPath() + "/findAll.product");
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("productMsg", e.getMessage());
            request.getRequestDispatcher("/findAll.product").forward(request, response);
        } catch (ProductNotExistException e) {
            e.printStackTrace();
            request.setAttribute("productMsg", e.getMessage());
            request.getRequestDispatcher("/findAll.product").forward(request, response);
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg", e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("productMsg", "服务器内部异常");
            request.getRequestDispatcher("/findAll.product").forward(request, response);

        }


    }
    public void modifyCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        //从session中取出购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String id=request.getParameter("id");
        String num=request.getParameter("num");
        try {
            cartService.modifyCart(cart,Integer.parseInt(id),Integer.parseInt(num));
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }
    public void removeById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        //从session中取出购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String id=request.getParameter("id");
        try {
            cartService.removeCart(cart,Integer.parseInt(id));
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");

        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }
    public void removeByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CartService cartService = (CartService) ObjectFactory.getObject("cartService");
        //从session中取出购物车
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        String[] ids=request.getParameterValues("id");
        try {
            for(String id:ids){
                cartService.removeCart(cart,Integer.parseInt(id));
            }
            response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            request.setAttribute("cartMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        } catch (UserNotLoginException e) {
            request.setAttribute("loginMsg",e.getMessage());
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("cartMsg","服务器内部异常");
            request.getRequestDispatcher("/jsp/cart.jsp").forward(request,response);
        }
    }
    public void clear(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().setAttribute("cart",new Cart());
        response.sendRedirect(request.getContextPath()+"/jsp/cart.jsp");
    }
}
